/*
 * Copyright (c) 2011, 2012, D'Bug and/or its affiliates. All rights reserved.
 * D'BUG PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 */
package robot;

import com.ibm.eNetwork.ECL.ECLErr;
import com.ibm.eNetwork.ECL.ECLOIA;
import com.ibm.eNetwork.ECL.ECLPS;
import com.ibm.eNetwork.ECL.ECLSession;
import com.ibm.eNetwork.HOD.customizable.CustomDesktop;
import com.ibm.eNetwork.HOD.customizable.HODDisplaySession;
import com.ibm.eNetwork.beans.HOD.Terminal;

/**
 * An applet is a small program that is intended not to be run on its own, but
 * rather to be embedded inside another application. <p> The
 * <code>Applet</code> class must be the superclass of any applet that is to be
 * embedded in a Web page or viewed by the Java Applet Viewer. The
 * <code>Applet</code> class provides a standard interface between applets and
 * their environment.
 *
 * @author Renan Alan Sousa Costa
 */
public class Term {

    CustomDesktop desktop = null;
    HODDisplaySession session = null;
    Terminal terminal = null;
    ECLSession eSession = null;
    ECLPS ps = null;
    ECLOIA oia;

    /**
     * Initialize session.
     *
     * @return
     * <code>Boolean.TRUE</code> if everything is correct;
     * <code>Boolean.FALSE</code> otherwise.
     */
    Boolean initialize() {
        try {
            session = desktop.startDisplaySession("SISBB Intranet");
            return Boolean.TRUE;
        } catch (Exception e) {
            System.out.println(e);
            return Boolean.FALSE;
        }
    }

    /**
     * Sleep in sisbb.
     *
     * @param x value for sleep.
     */
    public void sleep(Integer x) {
        try {
            Thread.sleep(x);
        } catch (InterruptedException e) {
            System.out.println(e);
        }
    }

    /**
     * Type text at sisbb.
     *
     * @param text the new requested width for the applet.
     * @param lin the new requested height for the applet.
     * @param col the new requested height for the applet.
     */
    public void type(String text, int lin, int col) {
        while (oia.InputInhibited() != ECLOIA.INHIBIT_NOTINHIBITED) {
            sleep(18);
        }
        try {
            ps.SendKeys(text, lin, col);
        } catch (ECLErr e) {
            System.out.println(e);
        }
    }

    public void type(String texto) {
        while (oia.InputInhibited() != ECLOIA.INHIBIT_NOTINHIBITED) {
            sleep(18);
        }
        try {
            ps.SendKeys(texto);
        } catch (ECLErr e) {
            System.out.println(e);
        }
    }

    /**
     * Copy information on sisbb.
     *
     * @param line line where information starts.
     * @param collumn column where information starts.
     * @param size size of string.
     */
    public String copy(int line, int column, int size) {
        char[] texto = new char[size + 1];
        try {
            ps.GetScreen(texto, texto.length + 1, line, column, size + 1, ECLPS.TEXT_PLANE);
        } catch (ECLErr e) {
            System.out.println(e);
        }
        return String.valueOf(texto, 0, size);
    }
}
